<?php $parent_menu = "个人信息";
$menu_selected = "我的信息修改"; ?>
@extends('web.frame_layout')
@section('content')
    <div class="m-right-nr">
        <div>
            <h2><span>我的资料</span></h2>
            @if( $user->first == 0)
                <p><strong>修改须知:</strong> 用户信息只能修改一次 请谨慎填写！</p>
            @elseif($user->first == 1)
                <p><strong>警告：</strong> 你已修改过一次资料，已不可再次修改，请联系管理员！</p>
            @endif
            <br>
            <div class="m-myPro">
                <form class="myPro" action="{{route('web.information.save',['id'=>(isset($user)?$user->id:0)])}}"
                      method="post" id="myPro" autocomplete="on" enctype="multipart/form-data">
                    <ul>
                        {{ csrf_field() }}
                        <li>
                            <label for="myTJ" class="hidden-xs">推广链接：</label>
                            <input type="text" id="to_copy" style="background-color: #EBEBE4" readonly
                                   value="{{route('reg.page').'/'.$user->no}}">
                            <input type="button" onClick="copyUrl()" value="复制按钮"
                                   class="btn btn-default" style="width: 100px;">
                            <p class="u-ts"></p>
                        </li>
                        <li>
                            <label for="myTJ" class="hidden-xs"><i>*</i>我的推荐人：</label>
                            <input type="text" id="myTJ" value="{{!empty($user->pid)?getUserNameById($user->pid):'无'}}"
                                   name="myTJ" placeholder="我的推荐人" disabled>
                            <p class="u-ts"></p>
                        </li>
                        <li>
                            <label for="myJD" class="hidden-xs"><i>*</i>我的接点人：</label>
                            <input type="text" id="myJD" value="{{!empty($user->nid)?getUserNameById($user->nid):'无'}}"
                                   name="myJD" placeholder="我的接点人" disabled>
                            <p class="u-ts"></p>
                        </li>
                        <li>
                            <label for="email" class="hidden-xs"><i>*</i>绑定邮箱：</label>
                            <input type="text" id="email" name="email" {{$user->first == 1 ? 'readonly' : ''}}
                            value="{{isset($user->email)?$user->email:''}}"
                                   placeholder="your email @ 163.com">
                            <p class="u-ts"></p>
                        </li>
                        <li>
                            <label for="realname" class="hidden-xs"><i>*</i>真实姓名：</label>
                            <input type="text" id="realname" name="realname" {{$user->first == 1 ? 'readonly' : ''}}
                            value="{{isset($user->realname)?$user->realname:''}}"
                                   placeholder="请输入您的真实姓名">
                            <p class="u-ts"></p>
                        </li>
                        <li>
                            <label for="mobile" class="hidden-xs"><i>*</i>联系方式：</label>
                            <input type="text" id="mobile" name="mobile" {{$user->first == 1 ? 'readonly' : ''}}
                            value="{{isset($user->mobile)?$user->mobile:''}}"
                                   placeholder="请填写常用号码">
                            <p class="u-ts"></p>
                        </li>
                        <input type="hidden" id="pic" name="pic" value="{{$user->pic}}">
                        <li class="filePic-li">

                            <label for="filePic" class="hidden-xs"><i>*</i>上传图片：</label>
                            <input type="file" id="filePic" name="filePic">

                            <div>
                                <p></p>
                                <h4></h4>
                            </div>
                            <p class="u-ts"></p>
                        </li>
                        <li class="filePic-li">
                            <label for="filePic" class="hidden-xs">推广二维码：</label>
                            <img src="{{route('produce.qrcode')}}" alt="">
                        </li>
                        <li>
                            <button>立即保存</button>
                        </li>
                    </ul>
                </form>
            </div>
        </div>
    </div>
@endsection

@section('footer')

    <script type="text/javascript">
        // 验证规则
        $('#myPro').validate({
            rules: {
                email: {
                    required: true,
                    email: true
                },
                realname: {
                    required: true,
                    maxlength: 5
                },
                mobile: {
                    required: true,
//                    tel: true
                }
            },
            messages: {
                email: {
                    required: "不能为空"
                },
                name: {
                    required: "不能为空"
                },
                tel: {
                    required: "不能为空"
                }
            },
            errorPlacement: function (error, element) {
                $(element).siblings('.u-ts').html(error)
            },
            errorElement: 'span',
            submitHandler: function (form) {
                $(form).ajaxSubmit({
                    dataType: "json",
                    type: "post",
                    success: function (data) {

                        if (data.code == 0) {
                            toastr["success"](data.msg);
                            setInterval(function () {
                                window.location = "{{route('web.information')}}";
                            }, 2000)
                        } else {
                            toastr["error"](data.msg);
                        }
                    }
                });
            }
        });

        //        jQuery.validator.addMethod('telNum', function (value, element) {
        //            var phoneNum = /^[1][34578][\d]{9}$/.test(value);
        //            return this.optional(element) || phoneNum
        //        }, '请输入正确手机号');

        var pic = $("#pic").val();

        $('.filePic-li>div').html('<img src="' + pic + '">');

        $('#filePic').on('change', function () {
            var reader = new FileReader();
            var files = this.files[0];
            reader.readAsDataURL(files);
            reader.onload = function () {
                if ((files.type === 'image/png' || files.type === 'image/jpeg') && files.size < 1169328)
                    $('.filePic-li>div').html('<img src="' + reader.result + '">')
            }
        });
    </script>

    <script type="text/javascript">
        function copyUrl() {
            var Url = document.getElementById("to_copy");
            Url.select(); // 选择对象
            document.execCommand("Copy"); // 执行浏览器复制命令
            toastr["success"]("复制成功");
        }
    </script>
@endsection